How many places target has - the places you can reach with target[i], from 0 up to one less than this. For an array made with array_create, or written out with a value for every place, that is the size you gave it.
array_length(target) -> int| Name | Type |
|---|---|
target | array |
int
Tells you how many places target, an array, has. The places are numbered from 0, so the first one is target[0] and the last one is target[array_length(target) - 1].
An array made with array_create(5, 0) has five places, so array_length gives 5.
Most of the time this is just the size you gave the array. The answer is the number of places the array really has, because that is the number [i] is checked against: ask for a place past it and the game stops with an "Invalid index" message.
| The array | array_length gives |
|---|---|
array_create(5, 0) | 5 |
written out in full, like [10, 20, 30, 40, 50] | 5 |
/// @capacity 5 over only three values, [10, 20, 30] | 3 |
The last row is the one to watch. The 5 in /// @capacity 5 is how much room the array has, but an array only gets the values you wrote, and the other two places do not exist (see Arrays). array_length tells you the truth: 3. Reading [3] would stop the game, and a loop that stops at array_length never tries.
This is what array_length is mostly for:
var scores = array_create(5, 0);
for (var i = 0; i < array_length(scores); i++) {
scores[i] = i * 10; // 0, 10, 20, 30, 40
}
var last = scores[array_length(scores) - 1]; // 40If you later change 5 to 8 in the array_create line, the loop still covers every place, because it asks the array instead of remembering a number.
A script that is given an array works on the caller's own array (see Arrays and Functions), and a script cannot see how big that array is by looking at it. array_length is how it finds out:
/// @arg {array<int>} values
function total(values) {
var sum = 0;
for (var i = 0; i < array_length(values); i++) {
sum += values[i];
}
return sum;
}The same script works for an array of 5 and an array of 50, and gives each its own length.
array_length takes an array and nothing else. Every container has its own way of counting:
| Container | Ask how big it is with |
|---|---|
| array | array_length |
| list | list_length |
| grid | grid_width and grid_height - a grid has two sizes |
| stack | stack_length |
| queue | queue_length |
| set | set_length |
| map | map_length |
Giving it the wrong kind is refused when the project is checked, and the message names the function that fits.
It costs next to nothing: it reads a number the array already keeps, and does not walk through the places.
Just the vector's size() - O(1), never walks the array. It is the number of places that EXIST, which is exactly what target[i] is checked against ("Invalid index" past it). That is the size you wrote down for an array made with array_create(n, ...) or written out in full; an array declared with more room than values (`/// @capacity 5` over a three-value literal) has only 3 places, and this says 3 - the capacity is only the most it could hold. Inside a script an array parameter is a reference to the capacity-free vector base, so this is the length of the array the CALLER passed in - the one thing a script has no other way to learn.